package com.edroid.autorunner;

import java.util.LinkedList;
import java.util.Queue;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.widget.TextView;

import com.edroid.apkhooker.R;
import com.edroid.autorunner.FloatViewManager.FloatView;

public class StateFloatButton implements Callback {
	private static StateFloatButton instance;

	private FloatView floatView;
	private boolean warning = false;
//	private SoundPool soundPool;
//	private int sid_warn;
	private Vibrator vibrator;
	private Context context;
	private TextView textView1, textView2, textView3, textView4, tv_count,
			tv_msg;
	private Handler mHandler;
	private int time, msgTime, count;
	private Queue<String> msgQueue = new LinkedList<String>();
	private State curState;

	public enum State {
		STA_STOP("已停止", R.drawable.btn_bg_normal), STA_NEW("新增",
				R.drawable.btn_bg_normal), STA_OLD("留存",
				R.drawable.btn_bg_yellow), STA_FINISH("完成",
				R.drawable.btn_bg_green), STA_SLEEP("已休眠",
				R.drawable.btn_bg_green), STA_IDLE("空闲",
				R.drawable.btn_bg_green), STA_NETERR("网络故障", R.drawable.btn_bg_red)
				, STA_ERR2("异常终止", R.drawable.btn_bg_red);

		private String text;
		private int bg;

		private State(String s, int i) {
			this.text = s;
			this.bg = i;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case 1000: {
			onTimer();

			mHandler.sendEmptyMessageDelayed(1000, 1000);
			break;
		}

		default:
			return false;
		}

		return true;
	}

	private void onTimer() {
		if (time > 0) {
			time--;
			setTimeText(String.valueOf(time));
		}
		msgTime++;
		if (msgTime < 5) {
			tv_msg.setText(msgQueue.poll());
		}
	}

	private StateFloatButton() {
	}

	public void setState(State s) {
		curState = s;
		setStateText(s.text);
		setBg(s.bg);

		if (s == State.STA_STOP) {
			// setTime(0);
		}
	}

	public boolean isRunInState(State s) {
		return curState == s;
	}

	public State getCurState() {
		return curState;
	}

//	private void soundLoad() {
//		soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
//		sid_warn = soundPool.load(context, R.raw.beep, 0);
//	}

	private void init(Context context) {
		this.context = context;
		// Start the vibration
		vibrator = (Vibrator) context
				.getSystemService(Context.VIBRATOR_SERVICE);
//		soundLoad();
	}

	public void setStateText(String text) {
		textView1.setText(text);
	}

	public void setChannelText(String text) {
		textView3.setText(text);
	}

	private void setTimeText(String text) {
		textView4.setText(text);
	}

	public void setTime(int time) {
		this.time = time;
		setTimeText(String.valueOf(time));
	}

	public void setBg(int resid) {
		floatView.rootView.setBackgroundResource(resid);
	}

	public void setCountText(int number) {
		tv_count.setText(String.valueOf(number));
	}

	public void setMsgText(String msg) {
		if(msgQueue.isEmpty()) {
			tv_msg.setText(msg);
		}else {
			msgTime = 0;
			msgQueue.add(msg);
		}
	}
	private static final long[] vibratePattern = {100, 200, 300, 400, 500, 600, 700};
	public void startWarn() {
		if (!warning) {
			warning = true;
//			soundPool.play(sid_warn, 0.1f, 0.1f, 0, 5, 1);

			// convert it using the function defined above. See the docs for
			// android.os.Vibrator for more info about the format of this array
			// long[] pattern = MorseCodeConverter.pattern("ACDEFGTGHYT");
			vibrator.vibrate(vibratePattern, 0);

//			setState(State.STA_ERR);
		}
	}

	public void stopWarn() {
		if (warning) {
			warning = false;
//			soundPool.stop(sid_warn);
			vibrator.cancel();

			textView1.setText("正在运行");
			floatView.rootView.setBackgroundResource(R.drawable.btn_bg_normal);
		}
	}

	public void onActivityCreate(Activity activity) {
		init(activity.getApplicationContext());

		if (floatView == null) {
			try {
				floatView = FloatViewManager.getInstance().create(activity,
						R.layout.floatbutton, new FloatView.OnClickListener() {

							@Override
							public void onClick() {
								// Intent intent =
								// context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
								// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								// context.startActivity(intent);
							}
						});

				View view = floatView.rootView;

				textView1 = (TextView) view.findViewById(R.id.textView1);
				textView2 = (TextView) view.findViewById(R.id.textView2);
				textView3 = (TextView) view.findViewById(R.id.textView3);
				textView4 = (TextView) view.findViewById(R.id.textView4);
				tv_count = (TextView) view.findViewById(R.id.tv_count);
				tv_msg = (TextView) view.findViewById(R.id.tv_msg);
				PackageInfo packageInfo = activity.getPackageManager()
						.getPackageInfo(activity.getPackageName(), 0);
				textView2.setText("V" + packageInfo.versionName);

				mHandler = new Handler(this);
				mHandler.sendEmptyMessage(1000);
			} catch (Exception e) {
			}
		}
	}

	public static StateFloatButton getInstance() {
		if (instance == null)
			instance = new StateFloatButton();

		return instance;
	}

	
	public void update() {
		count++;
		if ((count & 0x1) != 0) { // 第二位，达到交替更新
			textView2.setTextColor(Color.RED);
		} else {
			textView2.setTextColor(Color.WHITE);
		}
	}
}
